package com.vdong.common.oauth.service.impl;

import com.vdong.common.oauth.common.exception.WebBizException;
import com.vdong.common.oauth.entity.mo.WechatAuthorizerInfo;
import com.vdong.common.oauth.entity.mo.WechatOpenPlatform;
import com.vdong.common.oauth.repository.dao.WechatAuthorizerInfoMapper;
import com.vdong.common.oauth.repository.dao.WechatOpenPlatformMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author 何利庭
 * @description : 绑定开放平台
 * @creat 2018/11/16 13:56
 */
@Slf4j
@Service
public class WechatPlatformService {

    @Resource
    private WechatAuthorSerivce wechatAuthorSerivce;

    @Resource
    private WechatOpenPlatformMapper wechatOpenPlatformMapper;

    @Resource
    private WechatAuthorizerInfoMapper wechatAuthorizerInfoMapper;

    public WechatOpenPlatform getOpenPlatform(Long storeId) {
        List<WechatOpenPlatform> list = wechatOpenPlatformMapper.select(storeId);
        if (null == list || 0 == list.size()) {
            return null;
        }else {
            return list.get(0);
        }

    }

    public Boolean deleteOpenPlatform(Long id) {
        return wechatOpenPlatformMapper.updateByPrimaryKey(WechatOpenPlatform.builder()
                .id(id)
                .isEffective(Boolean.FALSE)
                .modifyDate(new Date()).build()) > 0;
    }

    public Boolean bindOpenPlatform(WechatAuthorizerInfo wechatAuthorizerInfo) {
        WechatOpenPlatform wechatOpenPlatform = getOpenPlatform(wechatAuthorizerInfo.getStoreId());
        //是否存在开放平台账号
        if (wechatOpenPlatform == null) {
            //重新绑定开放平台账号
//            if (!StringUtils.isEmpty(wechatAuthorizerInfo.getOpenId())) {
//                wechatAuthorSerivce.unbindOpenPlatform(wechatAuthorizerInfo.getAppid(), wechatAuthorizerInfo.getRefreshToken(),
//                        wechatAuthorizerInfo.getOpenId());
//                wechatAuthorizerInfo.setOpenId(null);
//            }
//            String openId = wechatAuthorSerivce.createOpenPlatform(wechatAuthorizerInfo.getAppid(),
//                    wechatAuthorizerInfo.getRefreshToken());
//            log.debug("开放平台账号创建 openId:{},wechatAuthorizerInfo:{}", openId, wechatAuthorizerInfo.toString());

            wechatOpenPlatform = WechatOpenPlatform.builder()
                    .storeId(wechatAuthorizerInfo.getStoreId())
                    .openId(wechatAuthorizerInfo.getOpenId())
                    .principalName(wechatAuthorizerInfo.getPrincipalName())
                    .isEffective(Boolean.TRUE)
                    .createDate(new Date()).build();
            wechatOpenPlatformMapper.insert(wechatOpenPlatform);
            wechatAuthorizerInfo.setOpenId(wechatOpenPlatform.getOpenId());
            return true;
        }
        //已绑定
        if (wechatOpenPlatform.getOpenId().equals(wechatAuthorizerInfo.getOpenId())) {
            return true;
        }
        //重新绑定 1.先解绑之前的开放平台账号
        if (!StringUtils.isEmpty(wechatAuthorizerInfo.getOpenId())) {
            //绑定了别人的开放平台账号
            wechatAuthorSerivce.unbindOpenPlatform(wechatAuthorizerInfo.getAppid(), wechatAuthorizerInfo.getRefreshToken(),
                    wechatAuthorizerInfo.getOpenId());
            wechatAuthorizerInfo.setOpenId(null);
        }
        //2.开放平台主体和应用主体是否一致
        if (!StringUtils.equals(wechatOpenPlatform.getPrincipalName(), wechatAuthorizerInfo.getPrincipalName())) {
            throw new WebBizException("开放平台主体和应用的主体不一致，无法授权，开放平台主体：" + wechatOpenPlatform.getPrincipalName());
        }
        //3.绑定开放平台账号
        wechatAuthorSerivce.bindOpenPlatform(wechatAuthorizerInfo.getAppid(), wechatAuthorizerInfo.getRefreshToken(), wechatOpenPlatform.getOpenId());
        wechatAuthorizerInfo.setOpenId(wechatOpenPlatform.getOpenId());
        return Boolean.TRUE;
    }
}
